package com.shop.controller;

import com.shop.model.Category;
import com.shop.model.Goods;
import com.shop.model.common.GoodsShowPage;
import com.shop.service.CategoryService;
import com.shop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
@RequestMapping("/")
public class IndexController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CategoryService categoryService;
    // 首页
    @RequestMapping({"/", "index"})
    public ModelAndView index() {
        ModelAndView modelAndView=new ModelAndView();
        List<Category> categoryList=categoryService.selectAllCategory();
        modelAndView.addObject("categoryList",categoryList);
        GoodsShowPage goodsShowPage=new GoodsShowPage();
        goodsShowPage.setPage(1);
        goodsShowPage.setPageSize(14);
        List<Goods> goodsList=goodsService.showPage(goodsShowPage);
        Integer count = goodsService.showPageCount(goodsShowPage);
        Integer totalPages=count/goodsShowPage.getPageSize()+1;
        if(count%goodsShowPage.getPageSize()==0) totalPages=count/goodsShowPage.getPageSize();
        modelAndView.addObject("goodsList", goodsList);
        modelAndView.addObject("pageTotal",totalPages);
        modelAndView.addObject("count",count);

        modelAndView.setViewName("index");

        return modelAndView;
    }
}
